﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MN.Framework.SharePoint.Membership
{
    public class UserOnline
    {
        public struct UserLive
        {
            public string SPUserName
            {
                get;
                set;
            }
            public string UserName
            {
                get;
                set;
            }
            public string FullName
            {
                get;
                set;
            }
        }
        public struct Message
        {
            public UserLive Sender
            {
                get;
                set;
            }
            public UserLive Resiver
            {
                get;
                set;
            }
            public string Content
            {
                get;
                set;
            }

        }
        static List<UserLive> UserLiveList = new List<UserLive>();
        static List<Message> MessageList = new List<Message>();
        public void AddUser(UserLive user)
        {
            if (!UserLiveList.Any(x => x.UserName == user.UserName))
            {
                user.UserName = user.UserName.ToLower();
                UserLiveList.Add(user);
            }
        }
        public void RemoveUser(string spUserName)
        {
            try
            {
                var userItem = UserLiveList.SingleOrDefault(x => x.SPUserName == spUserName);
                UserLiveList.Remove(userItem);
            }
            catch { }
        }
        public List<UserLive> GetAllUser()
        {
            return UserLiveList;
        }

        public void sendMessage(string sender, string resiver, string msg)
        {
            sender = sender.ToLower();
            if (UserLiveList.Any(u => u.UserName == sender))
            {
                var userSender = UserLiveList.SingleOrDefault(u => u.UserName == sender);
                var message = new Message() { Sender = userSender, Resiver = new UserLive() { UserName = resiver }, Content = msg };
                MessageList.Add(message);
            }

        }

        public void sendMessage(Message message)
        {
            MessageList.Add(message);
        }
        public List<Message> ResiveMessage(UserLive resiver)
        {
            return ResiveMessage(resiver.UserName);
        }
        public List<Message> ResiveMessage(string username)
        {
            var res = MessageList.Where(x => x.Resiver.UserName == username).ToArray().ToList();
            MessageList.RemoveAll(m => m.Resiver.UserName == username);
            return res;
        }
    }
}
